package com.ruoyi.web.controller.train;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.TrainingCadetAttendance;
import com.ruoyi.system.domain.TrainingCadetAttendanceReport;
import com.ruoyi.system.service.ITrainingCadetAttendanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 培训考勤Controller
 *
 * @author dp
 * @date 2023-11-27
 */
@Api(tags = {"培训考勤"})
@RestController
@RequestMapping("/train/attendance")
public class TrainingCadetAttendanceController extends BaseController {
    @Autowired
    private ITrainingCadetAttendanceService trainingCadetAttendanceService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询培训考勤列表
     */
    @ApiOperation(value = "培训考勤列表", notes = "查询培训考勤列表详情", tags = {"培训考勤管理"})
    //@PreAuthorize("@ss.hasPermi('train:attendance:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrainingCadetAttendance trainingCadetAttendance) {
        startPage();
        List<TrainingCadetAttendance> list = trainingCadetAttendanceService.selectTrainingCadetAttendanceList(trainingCadetAttendance);
        return getDataTable(list);
    }

    /**
     * 查询培训考勤报表
     */
    @ApiOperation(value = "培训考勤报表", notes = "查询培训考勤列表详情", tags = {"培训考勤管理"})
    @GetMapping("/listReport")
    public TableDataInfo listReport(TrainingCadetAttendanceReport trainingCadetAttendance) {
        startPage();
        List<TrainingCadetAttendanceReport> list = trainingCadetAttendanceService.selectTrainingCadetAttendanceReport(trainingCadetAttendance);
        return getDataTable(list);
    }

    /**
     * 查询培训考情明细
     * @param trainingCadetAttendance
     * @return
     */
    @ApiOperation(value = "培训考勤列表", notes = "查询培训考勤列表详情", tags = {"培训考勤管理"})
    //@PreAuthorize("@ss.hasPermi('train:attendance:list')")
    @GetMapping("/getAttendanceList")
    public TableDataInfo getAttendanceList(TrainingCadetAttendanceReport trainingCadetAttendance) {
        startPage();
        List<TrainingCadetAttendance> list = trainingCadetAttendanceService.getAttendanceList(trainingCadetAttendance);
        return getDataTable(list);
    }

    /**
     * 导出培训考勤列表
     */
    @ApiOperation(value = "培训考勤列表导出", notes = "导出培训考勤列表", tags = {"培训考勤管理"})
    //@PreAuthorize("@ss.hasPermi('train:attendance:export')")
    @Log(title = "培训考勤", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainingCadetAttendance trainingCadetAttendance) {
        List<TrainingCadetAttendance> list = trainingCadetAttendanceService.selectTrainingCadetAttendanceList(trainingCadetAttendance);
        ExcelUtil<TrainingCadetAttendance> util = new ExcelUtil<TrainingCadetAttendance>(TrainingCadetAttendance.class);
        util.exportExcel(response, list, "培训考勤数据");
    }

    /**
     * 获取培训考勤详细信息
     */
    @ApiOperation(value = "获取培训考勤详细信息", notes = "获取培训考勤详细信息详情", tags = {"培训考勤管理"})
    //@PreAuthorize("@ss.hasPermi('train:attendance:query')")
    @GetMapping(value = "/{attendanceId}")
    public AjaxResult getInfo(@PathVariable("attendanceId") Long attendanceId) {
        return success(trainingCadetAttendanceService.selectTrainingCadetAttendanceByAttendanceId(attendanceId));
    }

    /**
     * 新增培训考勤
     */
    @ApiOperation(value = "新增培训考勤", notes = "新增培训考勤详情", tags = {"培训考勤管理"})
    //@PreAuthorize("@ss.hasPermi('train:attendance:add')")
    @Log(title = "培训考勤", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody TrainingCadetAttendance trainingCadetAttendance) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        trainingCadetAttendanceService.completeData(trainingCadetAttendance, userId, 1);
        return toAjax(trainingCadetAttendanceService.insertTrainingCadetAttendance(trainingCadetAttendance));
    }

    /**
     * 修改培训考勤
     */
    @ApiOperation(value = "修改培训考勤", notes = "修改培训考勤详情", tags = {"培训考勤管理"})
    //@PreAuthorize("@ss.hasPermi('train:attendance:edit')")
    @Log(title = "培训考勤", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody TrainingCadetAttendance trainingCadetAttendance) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        trainingCadetAttendanceService.completeData(trainingCadetAttendance, userId, 2);
        return toAjax(trainingCadetAttendanceService.updateTrainingCadetAttendance(trainingCadetAttendance));
    }

    /**
     * 删除培训考勤
     */
    @ApiOperation(value = "删除培训考勤", notes = "删除培训考勤详情", tags = {"培训考勤管理"})
    //@PreAuthorize("@ss.hasPermi('train:attendance:remove')")
    @Log(title = "培训考勤", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attendanceIds}")
    public AjaxResult remove(@PathVariable Long[] attendanceIds) {
        return toAjax(trainingCadetAttendanceService.deleteTrainingCadetAttendanceByAttendanceIds(attendanceIds));
    }
}
